<?php
namespace App\Http\Controllers\ShopWeb;

use App\Http\Controllers\WebController;
use App\Http\Controllers\Business\ShopWeb\PublicBusiness;
use App\Http\Controllers\Business\ShopWeb\FinanceManagement\FinanceManagement;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Redis;

class FinanceController extends WebController
{
	//订单结款
    public function orderForPayment() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $totalResult = FinanceManagement::getOrderStatistic($shopIds);
        if($totalResult['status'] == 'success') {
            $totalWithdrawal = $totalResult['data']['totalwithdrawal'];
            $waitWithdrawal = $totalResult['data']['waitwithdrawal'];
            $alreadyWithdrawal = $totalResult['data']['alreadywithdrawal'];
        } else {
            $totalWithdrawal = 0.00;
            $waitWithdrawal = 0.00;
            $alreadyWithdrawal = 0.00;
        }
        $data = [
            'totalwithdrawal' => $totalWithdrawal,
            'waitwithdrawal' => $waitWithdrawal,
            'alreadywithdrawal' => $alreadyWithdrawal
        ];
        return view('shop.finance.order_for_payment', $data);
    }
    //订单列表
    public function orderList() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        if($this->request->has('starttime') && ($this->request->get('starttime') != '')) {
            $startTime = $this->request->get('starttime');
            $startTime = $startTime . " 00:00:00";
        } else {
            $startTime = '';
        }
        if($this->request->has('lasttime') && ($this->request->get('lasttime') != '')) {
            $lastTime = $this->request->get('lasttime');
            $lastTime = $lastTime . " 23:59:59";
        } else {
            $lastTime = '';
        }
        if($this->request->has('paginate') && ($this->request->get('paginate') != '')) {
            $paginate = $this->request->get('paginate');
        } else {
            $paginate = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $orderListResult = FinanceManagement::orderWithdrawalList($shopIds, $startTime, $lastTime,$page, $paginate);
        if($orderListResult['status'] == 'failed') {
            return $this->responseError($orderListResult['message']);
        }
        $data = $orderListResult['data'];
        return $this->responseSuccessData($data);
    }
    //地推结款
    public function salesmanForPayment() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $totalResult = FinanceManagement::getSalesmanStatistic($shopIds);
        if($totalResult['status'] == 'success') {
            $totalWithdrawal = $totalResult['data']['totalwithdrawal'];
            $waitWithdrawal = $totalResult['data']['waitwithdrawal'];
            $alreadyWithdrawal = $totalResult['data']['alreadywithdrawal'];
        } else {
            $totalWithdrawal = 0.00;
            $waitWithdrawal = 0.00;
            $alreadyWithdrawal = 0.00;
        }
        $data = [
            'totalwithdrawal' => $totalWithdrawal,
            'waitwithdrawal' => $waitWithdrawal,
            'alreadywithdrawal' => $alreadyWithdrawal
        ];
        return view('shop.finance.salesman_for_payment', $data);
    }
    //地推列表
    public function salesmanList() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        if($this->request->has('starttime') && ($this->request->get('starttime') != '')) {
            $startTime = $this->request->get('starttime');
            $startTime = $startTime . " 00:00:00";
        } else {
            $startTime = '';
        }
        if($this->request->has('lasttime') && ($this->request->get('lasttime') != '')) {
            $lastTime = $this->request->get('lasttime');
            $lastTime = $lastTime . " 23:59:59";
        } else {
            $lastTime = '';
        }
        if($this->request->has('paginate') && ($this->request->get('paginate') != '')) {
            $paginate = $this->request->get('paginate');
        } else {
            $paginate = '';
        }
        if($this->request->has('page')) {
            $page = $this->request->get('page');
        } else {
            $page = 1;
        }
        $orderListResult = FinanceManagement::salesmanList($shopIds,$startTime, $lastTime, $page, $paginate);
        if($orderListResult['status'] == 'failed') {
            return $this->responseError($orderListResult['message']);
        }
        $data = $orderListResult['data'];
        return $this->responseSuccessData($data);
    }
    //地推结款已提现详情
    public function salesmanForPaymentDetail() {
        $shopUser = Auth::guard('shopweb')->user();
        $shopUserId = $shopUser->id;
        $shopIdString = Redis::get('ShopWeb:ShopId:'. $shopUserId);
        if(!$shopIdString) {
            $shopIdString = $shopUser->shopid;
        }
        $shopIds = explode(',', $shopIdString);
        $shopSalesmanId = $this->request->get('shopsalesmanid');
        $result = FinanceManagement::getSalesmanDetail($shopIds, $shopSalesmanId);
        if($result['status'] == 'failed') {
            return view('errors.404');
        }
        $data = $result['data'];
        return view('shop.finance.salesman_detail', $data);
    }
}